package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.Service.CategoryService;
import com.ithiema.reggie.Service.SetmealDishService;
import com.ithiema.reggie.Service.SetmealService;
import com.ithiema.reggie.common.CustomException;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.dto.SetmealDto;
import com.ithiema.reggie.entity.Category;
import com.ithiema.reggie.entity.Setmeal;
import com.sun.scenario.effect.impl.prism.PrTexture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;



@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;
    //添加套餐
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> save (@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    //套餐分页
    @GetMapping("/page")
    public R<Page> page (int page,int pageSize,String name){

        Page<Setmeal> pageinfo = new Page<>(page,pageSize);
        Page<SetmealDto> resulpage = new Page<>();

        LambdaQueryWrapper<Setmeal> lwp = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        lwp.like(null!=name,Setmeal::getName,name);
        //添加排序条件，根据更新时间降序排列
        lwp.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo,lwp);

        //现在开始把两个类中需要的往合集类中转
        BeanUtils.copyProperties(pageinfo,resulpage,"records");
        List<Setmeal> records = pageinfo.getRecords();
        List<SetmealDto> resultList= new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            //查询当前套餐所属分类名称
            Category category = categoryService.getById(record.getCategoryId());
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            resultList.add(setmealDto);
        }
        resulpage.setRecords(resultList);
        return R.success(resulpage);
    }



    //删除套餐  {同时可以批量删除}
    //清除setmealCache名称下,所有的缓存数据
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> delete (@RequestParam List<Long> ids){
       setmealService.removeWithDish(ids);
        return R.success("删除成功");
   }

    //套餐的起售停售
    @PostMapping("/status/{status}")
    private R<String> sales(@PathVariable("status") Integer status, @RequestParam List<Long> ids){
            setmealService.salesWithDish(status,ids);
        return R.success("修改状态成功");
    }

    //套餐修改(根据id查询)
    @GetMapping("/{id}")
    private R<SetmealDto> getData(@PathVariable Long id){
        SetmealDto setmealDto= setmealService.getData(id);
        return R.success(setmealDto);
    }

    //套餐修改

    @PutMapping
    public R<String> edit(@RequestBody SetmealDto setmealDto){
        setmealService.edit(setmealDto);
        return R.success("添加成功");
    }



    //客户端,查询套餐   {根据id查询}
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId +'_'+ #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> lwp = new LambdaQueryWrapper<>();
        //比较id和状态
        lwp.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId())
                .eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        //排序:根据修改时间倒序
        lwp.orderByDesc(Setmeal::getUpdateTime);
        //只要类型是list  后面方法就是list
        List<Setmeal> list = setmealService.list(lwp);

        return R.success(list);
    }

}
